package com.srd.phone.mvp.presenters;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.srd.phone.application.HttpStringRequest;
import com.srd.phone.mvp.interfaces.IBaseView;
import com.srd.phone.utils.verifycode.CheckView;

import java.util.HashMap;
import java.util.Map;

public class LoginPresenter extends BasePresenter {

    public String verifyData = "";
    public boolean isShowCheck = false;

    public LoginPresenter(RequestQueue requestQueue, IBaseView iView0) {
        super(requestQueue, iView0);
    }

    // 初始化验证码并且刷新界面
    public void setCheckNum(String data, CheckView mCheckView) {
        if (TextUtils.isEmpty(data))
            return;
        verifyData = data;
        char[] checkNum = data.toCharArray();
        mCheckView.setCheckNum(checkNum);
        mCheckView.invaliChenkNum();
    }

    //登陆验证
    public String checkLogin(String user, String pwd, String checkValue) {
        if (TextUtils.isEmpty(user) || TextUtils.isEmpty(pwd)) {
            return "用户名或密码不能为空";
        } else if (isShowCheck && TextUtils.isEmpty(checkValue)) {
            return "验证码不能为空";
        } else if (isShowCheck && !verifyData.equalsIgnoreCase(checkValue)) {
            return "验证码输入错误，请重新输入";
        }
        return "";
    }

    //登陆
    public void requestLogin(final String url, final String input, final String pwd, final String verify) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("input", input);
                map.put("pwd", pwd);
                map.put("vc", verify);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    //获取验证码
    public void getVerifyData(final String url) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        });
        request.setTag(url);
        requestQueue.add(request);
    }
}
